<?php
require_once("inc.admin.php");

$this_title="$vars[admin_title] &raquo; ".__("Sales Record By Product");
$page_title=__("Sales Record By Product");

if(!$pv["task"]["Sales"]){
	$errmsg="<h2>$page_title</h2>".format_err(__("You do not have the privilege to access inventory record."));

	print format_admin_page($errmsg, $this_title);
	exit();
}

$item_per_page=100;//$vars['admin_item_per_page'];

//#####SALES GET#####
if($get_s['date_from'] || $get_s['date_to']){
	if(strlen($get_s['date_from'])){
		if(!verify_date($get_s['date_from'])){
			$errmsg.=__("Please provide a valid date in the date from.")."<br />";
		}
	}else{				
		$errmsg.=__("Please provide a valid date in the date to.")."<br />";			
	}
	
	if(strlen($get_s['date_to'])){
		if(!verify_date($get_s['date_to'])){
			$errmsg.=__("Please provide a valid date in the date to.")."<br />";
		}else{
			$from_time = strtotime($get_s['date_from']);
			$to_time = strtotime($get_s['date_to']);
			if($from_time > $to_time){
				$errmsg.=__("The from date cannot be later than the to date.")."<br />";			
			}		
		}	
	}else{				
		$errmsg.=__("Please provide a valid date in the date to.")."<br />";			
	}
	
	if(!$errmsg){
		if(strlen($from_time)){
			$from_date = date('Y-m-d', $from_time).' 00:00:00';
			if(strlen($to_time)){
				$to_date = date('Y-m-d', $to_time).' 23:59:59';
			}else{
				$to_date = date('Y-m-d', $from_time).' 23:59:59';
			}
			$date_q = "gg_purchase.cdate>='$from_date' and gg_purchase.cdate<='$to_date'";
		}else{
			$date_q = "1=1";
		}
		$record_sql = "SELECT gg_purchase_detail.refno, gg_purchase_detail.title, sum(gg_purchase_detail.qty) as qty, sum(gg_purchase.total_bv) as bv, sum(gg_purchase_detail.amount) as amount FROM gg_purchase INNER JOIN gg_purchase_detail ON gg_purchase_detail.slid=gg_purchase.id WHERE gg_purchase.status!='cancelled' and gg_purchase.total_bv>0 and ".$date_q." GROUP BY gg_purchase_detail.refno,gg_purchase_detail.title";
		$count_sql = "select count(*) from (".$record_sql.") tmp";
		
		$total_record=@mysql_result(mysql_query($count_sql), 0);
		$r=mysql_query($record_sql);				
		$k=0;		
		$run_no=1;		
		$sum_qty=0;		
		$sum_bv=0;		
		$sum_amount=0;		
		while ($product = mysql_fetch_assoc($r)) {			
			$qty_row.="		
				<tr class='row$k'>		
				<td>$run_no</td>		
				<td>".$product['refno']."</td>		
				<td>".$product['title']."</td>		
				<td align=right>".$product['qty']."</td>		
				<td align=right>".$product['bv']."</td>		
				<td align=right>".$product['amount']."</td>		
				</tr>";		
			
			$sum_qty+=$product['qty'];		
			$sum_bv+=$product['bv'];		
			$sum_amount+=$product['amount'];		
			$k=1-$k;		
			$run_no++;
		}
		
		//listing text
		$listing_text=replace_tag(__("Listing <%x%> product records."), array("<%x%>"=>strval($total_record)));
		
		//listing
		$qty_list=
		"<br />$listing_text
		<table class='aml_table'>
			<tr class='aml_header'>
				<td>".__("No.")."</td>
				<td>".__("Reference No.")."</td>
				<td>".__("Title")."</td>
				<td>".__("Qty")."</td>
				<td>".__("Total BV")."</td>
				<td>".__("Total Amount")."</td>
			</tr>
			$qty_row
			<tr class='aml_header'>
				<td colspan=3 align=right>".__("Total")."</td>
				<td align=right>".$sum_qty."</td>
				<td align=right>".number_format($sum_bv,2,'.','')."</td>
				<td align=right>".number_format($sum_amount,2,'.','')."</td>
			</tr>
		</table><br />";
		
		$record_sql = "SELECT date_format(cdate, '%Y-%m-%d') as cdate, sum(total_bv) as bv FROM gg_purchase WHERE gg_purchase.status!='cancelled' and gg_purchase.total_bv>0 and ".$date_q." GROUP BY date_format(cdate, '%Y-%m-%d')";
		$count_sql = "select count(*) from (".$record_sql.") tmp";
		
		$total_record=@mysql_result(mysql_query($count_sql), 0);
		$r=mysql_query($record_sql);				
		$k=0;		
		$run_no=1;		
		$sum_qty=0;		
		$sum_bv=0;		
		$sum_amount=0;		
		while ($product = mysql_fetch_assoc($r)) {			
			$qty_row2.="		
				<tr class='row$k'>		
				<td>$run_no</td>		
				<td>".$product['cdate']."</td>		
				<td align=right>".$product['bv']."</td>	
				</tr>";		
				
			$sum_bv+=$product['bv'];		
			$k=1-$k;		
			$run_no++;
		}
		
		//listing text
		$listing_text=replace_tag(__("Listing <%x%> daily sales records."), array("<%x%>"=>strval($total_record)));
		
		//listing
		$qty_list.=
		"<br />$listing_text
		<table class='aml_table'>
			<tr class='aml_header'>
				<td>".__("No.")."</td>
				<td>".__("Date")."</td>
				<td>".__("Total BV")."</td>
			</tr>
			$qty_row2
			<tr class='aml_header'>
				<td colspan=2 align=right>".__("Total")."</td>
				<td align=right>".number_format($sum_bv,2,'.','')."</td>
			</tr>
		</table>";
		//#####END QUANTITY LIST#####
	}
	$errmsg=$errmsg? format_err(__("There is some error(s), please correct them before continuing:")."<br />\n<br />\n$errmsg") : "";
}

//javascript
ob_start();
?>
<script type='text/javascript' src='<?php echo JS_URL."/get_file_gzip.php?file=".urlencode("jquery.js,jquery.datepicker.js"); ?>'></script>
<script type='text/javascript'>
jQuery(document).ready(function(j){
	j('select[@name=type]').change(function(){
		j('form[@name=comm_form]').submit();
	});
	
	<?php #datepicker ?>
	j('#date_from').datepicker({
		changeMonth: true,
		changeYear: true,
		dateFormat: 'yy-mm-dd',
		minDate: new Date(2009,5,1), 
		maxDate: '+1M +10D'
	});
	j('#date_to').datepicker({
		changeMonth: true,
		changeYear: true,
		dateFormat: 'yy-mm-dd',
		minDate: new Date(2009,5,1), 
		maxDate: '+1M +10D'
	});
});
</script>
<?php
$jvscript = ob_get_clean();

//css
ob_start();
?>
<link rel="stylesheet" href="<?php echo CSS_URL; ?>/jstheme/ui-lightness/jquery-ui.css" type="text/css" />
<?php
$css .= ob_get_contents();
ob_end_clean();

//listing
$prod_qty_list=($errmsg || $msg? $errmsg.$msg : "")."
<form name='qty_form' method='get' action='$this_file".($_SERVER["QUERY_STRING"]? "?".$_SERVER["QUERY_STRING"] : "")."'>
".__("Date Range").": <input type='text' name='date_from' id='date_from' value=\"".$get_h['date_from']."\" /> to <input type='text' name='date_to' id='date_to' value=\"".$get_h['date_to']."\" />&nbsp;<input type='submit' value=\"".__("Submit")."\" />
</form><br />\n
$qty_list";

$content="<h2>$page_title</h2>$prod_qty_list";

print format_admin_page($content, $this_title, $css . $jvscript);
?>